package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.UserDao;
import com.sls.nzdapplication.model.User;
import com.sls.nzdapplication.app.MySharedPreferences;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDataUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private com.sls.nzdapplication.model.User User;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public UserDataUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertUser(User user) {
        user.setCreateBy(username);
        user.setCreateTime(new Date());
        manager.getDaoSession().insertOrReplace(user);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultUser(final List<User> users) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (User user : users) {
                    user.setCreateBy(username);
                    user.setCreateTime(new Date());
                    manager.getDaoSession().insertOrReplace(user);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultUser2(final List<User> users) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (User user : users) {
                    manager.getDaoSession().insertOrReplace(user);
                }
            }
        });
    }


    /**
     * 删除数据
     */
    public void deleteUser(User user) {
        manager.getDaoSession().delete(user);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateUser(User user) {
        user.setModifyBy(username);
        user.setModifyTime(new Date());
        manager.getDaoSession().update(user);
    }

    /**
     * 按照主键返回单条数据
     */
    public User listOneUser(String key) {
        return manager.getDaoSession().load(User.class, key);
    }


    public List<User> queryUserByAreaId(String id){
        QueryBuilder<User> builder = manager.getDaoSession().queryBuilder(User.class);
        List<User> users = builder.where(UserDao.Properties.AreaId.eq(id)).orderDesc(UserDao.Properties.CreateTime).list();
        return  users;
    }

    public User queryUserByAreaIdAndUserCode(String id,String code){
        QueryBuilder<User> builder = manager.getDaoSession().queryBuilder(User.class);
        User users = builder.where(builder.and(UserDao.Properties.AreaId.eq(id),UserDao.Properties.BarCode.eq(code))).orderDesc(UserDao.Properties.CreateTime).unique();
        return  users;
    }

    public User queryUserByAreaIdAndUsername(String id,String code){
        QueryBuilder<User> builder = manager.getDaoSession().queryBuilder(User.class);
        User users = builder.where(builder.and(UserDao.Properties.AreaId.eq(id),UserDao.Properties.Username.eq(code))).orderDesc(UserDao.Properties.CreateTime).unique();
        return  users;
    }

    public User queryUserByAreaIdAndUserC(String id,String code){
        QueryBuilder<User> builder = manager.getDaoSession().queryBuilder(User.class);
        User users = builder.where(builder.and(UserDao.Properties.AreaId.eq(id),UserDao.Properties.UserCode.eq(code))).orderDesc(UserDao.Properties.CreateTime).unique();
        return  users;
    }

    public User queryUserByAreaIdAndAddress(String id,String code){
        QueryBuilder<User> builder = manager.getDaoSession().queryBuilder(User.class);
        User users = builder.where(builder.and(UserDao.Properties.AreaId.eq(id),UserDao.Properties.Address.eq(code))).orderDesc(UserDao.Properties.CreateTime).unique();
        return  users;
    }

    /**
     * 查询全部数据
     */
    public List<User> queryAll() {
        return manager.getDaoSession().loadAll(User.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<User> Users = queryAll();
        for(User User :Users){
            jsonArray.add(User.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<User> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new User(jsonArray.getJSONObject(i)));
        }
        insertMultUser2(list);
    }
}
